﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Ecreate.Popedom;

namespace Ecreate.Web.popedom.usersearch
{
    public partial class customersearch : System.Web.UI.Page
    {
        Ecreate.Popedom.Service.IGroupService groupservice = new Ecreate.Popedom.Service.SpringImpl.GroupServiceImpl();
        Ecreate.Popedom.Service.SpringImpl.UseStateImpl usestate = new Ecreate.Popedom.Service.SpringImpl.UseStateImpl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Ecreate.Popedom.tSysuser s = new tSysuser();

                this.ddl_usestate.DataSource = usestate.getAllState();
                this.ddl_usestate.DataTextField = "Name";
                this.ddl_usestate.DataValueField = "Id";
                this.ddl_usestate.DataBind();

                GridHelperSearch search = new GridHelperSearch();
                string fields = "prvoincename,cityname,countryname,level_one_userid,level_one_username,level_two_userid,level_two_username,cus_id,cus_name,cus_address,cus_tel,cus_linkman,cus_account,createdate,Canceldate,useflagname";
                string captions = "省,市,区县,省一级主管帐号,省一级主管姓名,地级市二级业务员帐号,地级市二级业务员姓名,销售商账号,销售商姓名,联系地址,联系电话,联系人,账号,创建日期,废止日期,用户状态";
                search.setGridColumn(this.GridView1, fields, captions);
            }

        }

        private string stringname(string value)
        {
            return value.Replace("1", ".");

        }

        private string stringReplace(string value)
        {


            string result = value.Replace("'", "");
            result = value.Replace("{", "");
            result = value.Replace("}", "");
            result = value.Replace("\"", "");
            return result;
        }
        private string stringReplace(object value)
        {
            return stringReplace(value.ToString());

        }
        protected void btn_action_Click(object sender, EventArgs e)
        {
            string jsondata = HtmlHelper.getPostValue(this);
            List<JsonSearch> list = JsonSearch.getList(jsondata);
            string hql = "  select t.*,case t.useflag when 1 then '有效' when 0 then '无效' end as useflagname       ,p.areaname as prvoincename,c.areaname as cityname,country.areaname as countryname,user1.username as level_one_username,user2.username as level_two_username  from t_customer  t left outer join t_area p on left(t.areacode,2)=p.areacode left outer join t_area c on left(t.areacode,4)=c.areacode left outer join t_area country on t.areacode=country.areacode  " +
                         "  left outer join t_sysuser user1 on t.level_one_userid=user1.userid left outer join  t_sysuser user2  on t.level_two_userid=user2.userid  where 1=1   ";
            string pre = " and  ";// " and  t.";
            foreach (JsonSearch js in list)
            {
                if (js.Value != "" && js.Value != null)
                {
                    hql += pre + js.Field + js.Operate + "'" + js.Value + "'";
                }
            }

            utility.SessionObject s = utility.SessionManage.getSession(this);

            string level = s.Admin.FLevel.ToString();
            string levelid = s.Admin.Id;
            //地级主管
            if (level == "1")
            {
                hql += "  and (   t.level_one_userid='" + levelid + "'";
                hql += "   or (   t.level_two_userid in (select userid  from t_sysuser where parentuser='"+levelid+"')   "   ;
                hql += ") )";
            }
            //
            if (level == "2")
            {
                hql += " and level_two_userid='" + levelid + "'";
            }
            System.Data.DataTable dt = Ecreate.Data.FactoryDBEngine.GetInstance().GetDataTable(hql);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

    


    }
}
